package GUI;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import web.PopulateShow;

import GUI.Show.ShowController;
import IO.Save;

import data.Show;

public class LaunchController implements ActionListener
{
	private Vector<Show> showVector;
	private LaunchMenu mainFrame;
	private JList showList;
	private JPanel mainPanel;
	private JButton displayShowButton;
	private JTextField addShowTextField;
	private DefaultListModel  listModel;
	
	public LaunchController(String launchName)
	{		
		this.mainFrame = new LaunchMenu(launchName);
		this.mainFrame.setResizable(false);
		this.mainPanel = new JPanel();
		JPanel displayShowPane = new JPanel();
		this.addShowTextField = new JTextField();
		
		//Display Show
		listModel = new DefaultListModel();
		for(Show s: ShowList)
		{
			listModel.addElement(s.getShowName());
		}
		this.showList = new JList(listModel);
		JScrollPane scrollPane = new JScrollPane(this.showList);
		this.displayShowButton = new JButton("Display Show");
		this.displayShowButton.addActionListener(this);
		this.displayShowButton.setActionCommand("Display Selected Show");

		//Add Show
		JButton addShowButton = new JButton("Add Show");
		addShowButton.addActionListener(this);
		addShowButton.setActionCommand("Add Show");
		
		//Rem Show
		JButton delShowButton = new JButton("Remove Show");
		delShowButton.addActionListener(this);
		delShowButton.setActionCommand("Delete Show");
		
		//Combine
		/*
		addShowPane.setLayout(new GridLayout(1,2));
		addShowPane.add(this.addShowTextField);
		addShowPane.add(addShowButton);
		*/
		displayShowPane.setLayout(new GridLayout(1,2));
		displayShowPane.add(scrollPane);		
		
		
		
		JPanel displayButtonPane = new JPanel();
		displayButtonPane.setLayout(new GridLayout(4,1));
		displayButtonPane.add(this.displayShowButton);
		displayButtonPane.add(delShowButton);
		displayButtonPane.add(this.addShowTextField);
		displayButtonPane.add(addShowButton);
		
		displayShowPane.add(displayButtonPane);
		
		this.mainPanel.setLayout(new GridLayout(1, 2));
		this.mainPanel.add(displayShowPane);
		//this.mainPanel.add(addShowPane);
		
		this.mainFrame.add(this.mainPanel);
		
		new LaunchView(this.mainFrame);
		//Fit and start
		
	}
	
	public Vector<String> getShowNames(Vector<Show> showVector)
	{
		Vector<String> returnVector = new Vector<String>();
		for(Show s: showVector)
		{
			returnVector.add(s.getShowName());
		}
		return returnVector;
		
	}
	
	public void updateList()
	{
		int showVectorSize = showVector.size();
		String[] showNames = new String[showVectorSize];
		for(int i=0; i<showVectorSize; i++)
		{
			showNames[i] = showVector.get(i).toString(); 
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if("Display Selected Show" == e.getActionCommand())
		{
			Object obj = this.showList.getSelectedValue();
			if(obj instanceof String)
			{
				String stringObj = (String) obj;
				for(Show s: showVector)
				{
					if(s.getShowName() == stringObj)
					{
						Point startP = new Point(this.mainFrame.getX()+this.mainFrame.getWidth(), this.mainFrame.getY());
						new ShowController(s, startP);
						break;
					}
				}
			}
		}
		else if("Delete Show" == e.getActionCommand())
		{
			Object obj = this.showList.getSelectedValue();
			if(obj instanceof String)
			{
				this.removeShow((String)obj);
			}
		}
		else if("Add Show" == e.getActionCommand())
		{
			boolean notInList = true;
			String showString = this.addShowTextField.getText();
			if(!showString.isEmpty())
			{
				for(Show s : showVector)
				{
					if(s.getShowName() == showString)
					{
						JOptionPane.showMessageDialog(null,
							    "There already is a show named "+showString,
							    "Inane error",
							    JOptionPane.ERROR_MESSAGE);
						notInList = false;
						break;

					}
				}
				if(notInList)
				{
					PopulateShow popShow = new PopulateShow(showString);
					Show testShow1 = popShow.getShow();
					testShow1.setPreferedSelectedEpisode(true, true);
					popShow = null;
					if(testShow1 == null)
					{
						JOptionPane.showMessageDialog(null,
							    "Could not add show "+showString,
							    "Inane error",
							    JOptionPane.ERROR_MESSAGE);			
					}
					else
					{
						showVector.add(testShow1);
						this.listModel.addElement(testShow1.getShowName());
						JOptionPane.showMessageDialog(null, showString +" was added.");
					}
					this.mainFrame.pack();
				}
			}
			this.addShowTextField.setText("");
			
		}
		Save.saveShows();
	}
	
	public void removeShow(String showName)
	{
		boolean wasFoundInList = this.listModel.removeElement(showName);
		boolean wasFoundInVector = false;
		this.mainFrame.pack();
		if(wasFoundInList)
		{
			wasFoundInVector = this.showVector.remove(this.getShowFromName(showName));
		}
		
		if(wasFoundInVector)
		{
			JOptionPane.showMessageDialog(null, showName +" was removed.");
		}
		else
		{
			JOptionPane.showMessageDialog(null,
				    "Could not find show "+ showName +" to remove.",
				    "Inane error",
				    JOptionPane.ERROR_MESSAGE);	
		}
		
	}
	
	public Show getShowFromName(String showName)
	{
		Show returnShow = null;
		for(Show s : this.showVector)
		{
			if(s.getShowName() == showName)
			{
				returnShow = s;
				break;
			}
		}
		return returnShow;
	}
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
    	Vector<Show> showVector = new Vector<Show>();
    	for(int i=1; i<=30; i++)
    	{
    		showVector.add(new Show("Show "+i));
    	}
        new LaunchController(showVector, "Sonny v2");
    }
}
